package com.adms.controller;

import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.adms.domain.User;
import com.adms.service.UserService;

@RequestMapping("user")
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping("{userId}")
	public Object getByUserId(@PathVariable("userId") Integer userId) {
		return userService.getByUserId(userId);
	}

	@GetMapping
	public Object list(User user, String timeField, Date start, Date end, String orderByClause, String key,
			@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize,
			@RequestParam(defaultValue = "5") Integer navigatePages) {
		return userService.list(user, timeField, start, end, orderByClause, key, pageNum, pageSize, navigatePages);
	}

	@PostMapping("login")
	public Object login(@RequestParam("phone") String phone, @RequestParam("password") String password,
			@RequestParam(defaultValue = "false") boolean rememberMe) {
		return userService.login(phone, password, rememberMe);
	}

	@PostMapping("logout")
	public Object logout() {
		return userService.logout();
	}

	@GetMapping("wechatLogin")
	public Object wechatLogin(@RequestParam("code") String code) {
		return userService.wechatLogin(code);
	}

	@PostMapping("wechatBind")
	public Object wechatBind(@RequestParam("phone") String phone, @RequestParam("sex") Integer sex,
			@RequestParam("headImage") String headImage) {
		return userService.wechatBind(phone, sex, headImage);
	}

	@GetMapping("checkPhone")
	public Object checkPhone(@RequestParam("phone") String phone) {
		return userService.checkPhone(phone);
	}

	@PostMapping("create")
	public Object create(User user) {
		return userService.create(user);
	}

	@PostMapping("createByFile")
	public Object createByFile(MultipartFile file) {
		return userService.createByFile(file);
	}

	@PostMapping("update")
	public Object update(User user) {
		return userService.update(user);
	}

	@PostMapping("delete")
	public Object delete(@RequestParam("userIds") int[] userIds) {
		return userService.delete(userIds);
	}

	@RequiresAuthentication
	@GetMapping("checkCookie")
	public Object checkCookie() {
		return true;
	}

	/**
	 * 验证密码
	 * 
	 * @param password 密码
	 * @return 布尔值
	 */
	@RequiresAuthentication
	@GetMapping("checkPassword")
	public Object checkPassword(@RequestParam("password") String password) {
		return userService.checkPassword(password);
	}

	/**
	 * 重置密码
	 * 
	 * @param phone    手机号
	 * @param password 密码
	 * @return 布尔值
	 */
	@GetMapping("resetPassword")
	public Object resetPassword(String phone, @RequestParam("password") String password) {
		return userService.resetPassword(phone, password);
	}

	@GetMapping("getTransferUsers")
	public Object getTransferUsers(@RequestParam("siteId") Integer siteId,
			@RequestParam("gridUserId") Integer gridUserId, @RequestParam("handleUserId") Integer handleUserId) {
		return userService.getTransferUsers(siteId, gridUserId, handleUserId);
	}

	@GetMapping("getSiteRelativeUsers")
	public Object getSiteRelativeUsers(@RequestParam("siteId") Integer siteId) {
		return userService.getSiteRelativeUsers(siteId);
	}

	@GetMapping("getDefaultGridUsers")
	public Object getDefaultGridUsers() {
		return userService.getDefaultGridUsers();
	}

}
